package com.example.administrator.leddemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.cbtService.AndroidSDK.LedService;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageButton im1,im2,im3,im4;
    private TextView tv1,tv2,tv3,tv4;
    private static boolean iflag1=true;//灯亮灭状态
    private static boolean iflag2=true;
    private static boolean iflag3=true;
    private static boolean iflag4=true;
    LedService led;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        led = new LedService();
        init();
    }
private void init()
{
    im1=(ImageButton)findViewById(R.id.imageButton4);
    im2=(ImageButton)findViewById(R.id.imageButton3);
    im3=(ImageButton)findViewById(R.id.imageButton2);
    im4=(ImageButton)findViewById(R.id.imageButton);
    tv1=(TextView)findViewById(R.id.textView5);
    tv2=(TextView)findViewById(R.id.textView4);
    tv3=(TextView)findViewById(R.id.textView3);
    tv4=(TextView)findViewById(R.id.textView2);
    im1.setOnClickListener(this);
    im2.setOnClickListener(this);
    im3.setOnClickListener(this);
    im4.setOnClickListener(this);
}
    @Override
    public void onClick(View v) {
switch(v.getId())
{
    case R.id.imageButton4:

        System.out.println("led1 按下");
        if (iflag1){
            led._setLedState(0,1);
            im1.setImageResource(R.drawable.ledon);

            iflag1=false;
        }
        else {
            led._setLedState(0,0);
            im1.setImageResource(R.drawable.ledoff);
            iflag1=true;
        }
        break;
    case R.id.imageButton3:
        System.out.println("led2 按下");
        if (iflag2){
            led._setLedState(1,1);
            im2.setImageResource(R.drawable.ledon);
            iflag2=false;
        }
        else {
            led._setLedState(1,0);
            im2.setImageResource(R.drawable.ledoff);
            iflag2=true;
        }
        break;
    case R.id.imageButton2:
        System.out.println("led3 按下");
        if (iflag3){
            led._setLedState(2,1);
            im3.setImageResource(R.drawable.ledon);
            iflag3=false;}
        else {
            led._setLedState(2,0);
            im3.setImageResource(R.drawable.ledoff);
            iflag3=true;}
        break;
    case R.id.imageButton:
        System.out.println("led4 按下");
        if (iflag4){
            led._setLedState(3,1);
            im4.setImageResource(R.drawable.ledon);
            iflag4=false;}
        else {
            led._setLedState(3,0);
            im4.setImageResource(R.drawable.ledoff);
            iflag4=true;}
        break;
}
    }
}

